home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / subdir.cpp < prev    next >
C/C++ Source or Header  |  1991-05-06  |  3KB  |  156 lines

  1. // SUBDIR.CPP - carry out a DOS command in all subdirectories of current one
  2. //        to compile use: tcc -ms subdir dblibs.lib
  3. //
  4. static char help[] = 
  5. "SUBDIR  cmd  - program carries out cmd on all subdirs of current dir"
  6. "\n               specify \'SUBDIR # cmd\' to act on subdirs themselves"
  7. "\n               ex: \'SUBDIR erase *.bak\' to erase all backup files"
  8. "\n                   \'SUBDIR # rd \' to remove all empty subdirectories"
  9. "\n by D BLUM\n";
  10.  
  11.  
  12. #include <dos.h>
  13. #include <dir.h>
  14. #include <sys\stat.h>
  15. #include <signal.h>
  16.  
  17. #include "dblib.h"
  18.  
  19.  
  20. /*  */
  21.  
  22.  
  23. // redefining wmalloc() and wrealloc() allow the Vlist and String routines
  24. //        to work without linking in the whole windows package.
  25. extern "C" void *wmalloc ( size_t n, char *msg )
  26.     {
  27.     void *ptr = malloc ( n );
  28.     _NORMALIZE ( ptr, void* );
  29.     if ( ptr == NULL && msg != NULL )
  30.         {
  31.         puts ( "\nOut of memory" );
  32.         exit (1);
  33.         }
  34.     return ptr;
  35.     }
  36.  
  37.  
  38. extern "C" void *wrealloc  ( void *block,  size_t size,  char *errmsg )
  39.     {
  40.     void *ptr = realloc ( block, size );
  41.     _NORMALIZE ( ptr, void* );
  42.     if ( ptr == NULL && errmsg != NULL )
  43.         {
  44.         puts ( "\nOut of memory" );
  45.         exit (1);
  46.         }
  47.     return ptr;
  48.     }
  49.  
  50.  
  51.  
  52.  
  53. static void dirVlist ( Vlist &vl );        // create Vlist of subdirectories
  54. static void process_dir ( void );        // loop thru subdirs and this dir
  55. static String command="";
  56. static int  dir_flag =0;                // command acts on subdirs 
  57.  
  58. static void ctrlbrk ( int sig )            // control break handler
  59.     {
  60.     exit (sig);
  61.     return;
  62.     }
  63.  
  64.  
  65. main ( int argc, char **argv )
  66.     {
  67.     if ( argc == 1 )
  68.         {
  69.         puts ( help );
  70.         exit (1);    
  71.         }
  72.     if ( SIG_ERR == signal ( SIGINT, ctrlbrk ) )
  73.         {
  74.         puts ( "ERROR setting control-break status" );
  75.         exit (1);
  76.         }
  77.         
  78.     if ( argv[1][0] == '#' )
  79.         {
  80.         dir_flag = ON;
  81.         argv[1] = " ";        // skip past the # 
  82.         }
  83.         
  84.     // BUILD COMMAND
  85.     for ( int narg=1; narg<argc; ++narg )
  86.         {
  87.         command += argv[narg];
  88.         command += " ";
  89.         }        
  90.         
  91.     process_dir ();
  92.     
  93.     return (0);    
  94.     }
  95.  
  96.  
  97. static void process_dir (void)
  98.     {
  99.     Vlist vl;
  100.     char  cwd[MAXPATH +1];
  101.     String  msg;
  102.     String  full_cmd;
  103.     
  104.     getcwd ( cwd, MAXPATH );
  105.     
  106.     dirVlist (vl);        // list all dirs in the current dir;
  107.  
  108.     if ( vl.count() > 0 )
  109.     for ( char **newdir= vl; *newdir != NULL; ++newdir )
  110.         {
  111.         chdir ( *newdir );
  112.         process_dir ( );
  113.         chdir ( cwd );
  114.         } 
  115.     
  116.     msg  =  "\nSUBDIR:    " ;
  117.     msg += cwd;
  118.     msg += "> ";
  119.     full_cmd = command;
  120.     if ( dir_flag )
  121.         {
  122.         full_cmd += cwd;    
  123.         chdir ( ".." );        // work in parent to act on this directory
  124.         } 
  125.     msg += full_cmd;    
  126.     puts ( msg );
  127.  
  128.     system( full_cmd );
  129.     
  130.     return;        // process_dir ()    
  131.     }
  132.     
  133. static void dirVlist ( Vlist &vl )
  134.     {
  135.     char far *dta;
  136.     struct ffblk  ffb;
  137.     char *dirname;    
  138.     int retcode;    
  139.     
  140.     dta =getdta();
  141.     
  142.     for (     retcode = findfirst ( "*.*", &ffb, FA_DIREC );
  143.             retcode != -1;
  144.             retcode = findnext ( &ffb ) 
  145.         )
  146.             {
  147.             // stack only directories, but avoid current and root.
  148.             if ( ( ffb.ff_attrib & FA_DIREC ) && ( ffb.ff_name[0] != '.' ) )
  149.                 {
  150.                 vl.push ( ffb.ff_name );
  151.                 }
  152.             }    
  153.     
  154.     setdta ( dta );
  155.     return;
  156.     }